package com.zzw.testdemo01.item;

import net.minecraft.command.arguments.EntityArgument;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.entity.merchant.villager.VillagerEntity;
import net.minecraft.item.*;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.world.server.ServerWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VillagerStick extends SwordItem {
    private static final Logger log = LoggerFactory.getLogger(VillagerStick.class);

    public VillagerStick() {
        super(ItemTier.DIAMOND, 8, -2.4F,
                new Properties().tab(ItemGroup.TAB_BUILDING_BLOCKS));
        // 8是攻击伤害，-2.4F是攻击速度
    }


    /**
     * 是否对敌人造成伤害，并施加虚弱II效果，被雷劈
     * @param itemStack 使用的物品堆栈
     * @param attacker 受害者实体
     * @param victim 攻击者实体
     * @return 是否成功伤害敌人
     */
    @Override
    public boolean hurtEnemy(ItemStack itemStack, LivingEntity attacker, LivingEntity victim) {
        if (attacker instanceof VillagerEntity){
            VillagerEntity villagerEntity = (VillagerEntity) attacker;
            reduceHealth(villagerEntity);
            modifyVillagerTrades(villagerEntity);
        }
        return true;
    }

    private void reduceHealth(VillagerEntity villagerEntity) {
        villagerEntity.setHealth(villagerEntity.getHealth() - 5);
    }

    private void modifyVillagerTrades(VillagerEntity villager) {

        MerchantOffers offers = villager.getOffers();
        MerchantOffers newOffers = new MerchantOffers();
        offers.forEach(offer -> {

            ItemStack costA = offer.getCostA();
            ItemStack costB = offer.getCostB();
            int maxUses = offer.getMaxUses();
            if (costB.isEmpty()) {
                ItemStack result = offer.getResult();
                MerchantOffer newTrade = new MerchantOffer(
                        costA, // 购买物品
                        result, // 出售物品
                        maxUses, // 最大交易次数
                        offer.getXp(), // 经验值增加
//                        offer.getPriceMultiplier() // 价格折扣
                        0.8F // 价格折扣
                );

                newOffers.add(newTrade);
            }else{
                newOffers.add(offer);
            }
        });

        // 添加新的交易
        villager.setOffers(newOffers);
    }

}
